import { Controller } from "../../mvc/core/Controller";
import { SArenaGroup } from "../../../Message/Public/SArenaGroup";
import { IArenaModule } from "../../../modules/interfaces/IArenaModule";
import { IView } from "../../mvc/interfaces/IView";
import { ArenaModule } from "../../../modules/ArenaModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { JoinData } from "../view/arena/ArenaData/JoinData";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Alert } from "../../../com/mui/controls/Alert";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { LaterUtil } from "../../common/LaterUtil";
import { PageData } from "../view/arena/ArenaData/PageData";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { SArenaOpen } from "../../../Message/Public/SArenaOpen";
import { ArenaDetailControler } from "./sub/ArenaDetailControler";
import { SArenaMatch } from "../../../Message/Public/SArenaMatch";
import { SArenaStart } from "../../../Message/Public/SArenaStart";
import { SArenaBattles } from "../../../Message/Public/SArenaBattles";
import { SArenaWaitingNum } from "../../../Message/Public/SArenaWaitingNum";
import { SArenaFinaRings } from "../../../Message/Public/SArenaFinaRings";
import { SArenaSignUpState } from "../../../Message/Public/SArenaSignUpState";
import { SArenaResult } from "../../../Message/Public/SArenaResult";
import { SArenaMoney } from "../../../Message/Public/SArenaMoney";
import { SArenaMode } from "../../../Message/Public/SArenaMode";
type int = number;
//class ArenaController
    
    export  class ArenaController extends Controller
    {
       

        public isInitView(): Boolean
        {
            return !(this._arenaModule == null);
        }

        /* internal  */onArenaGroupCommandHandler(arg1: SArenaGroup): void
        {
            (view as IArenaModule).updateGroupView(arg1);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._arenaModule) 
            {
                this._arenaModule = new ArenaModule();
            }
            return this._arenaModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.showArenaDetailPanel, this.opentArenaDetailPanelHander);
            Dispatcher.addEventListener(EventName.ArenaEnterWaitRoom, this.onEnterWaitRoom);
            Dispatcher.addEventListener(EventName.ArenaJoin, this.onArenaJoin);
            Dispatcher.addEventListener(EventName.ArenaGetMyArenaInfo, this.onGetMyArenaInfo);
            Dispatcher.addEventListener(EventName.ArenaIsAccept, this.onIsAccept);
            Dispatcher.addEventListener(EventName.ArenaShowArenaWindow, this.onShowArenaWindow);
            Dispatcher.addEventListener(EventName.ArenaExit, this.onArenaExit);
            Dispatcher.addEventListener(EventName.ArenaExitNow, this.onArenaExitNow);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.ArenaRefreshTopList, this.onRefreshTopList);
            Dispatcher.addEventListener(EventName.ArenaGetArenaBattles, this.onGetArenaBattles);
            Dispatcher.addEventListener(EventName.ArenaLookBattle, this.onLookBattle);
            Dispatcher.addEventListener(EventName.ArenaGetFinaRings, this.onGetFinaRings);
            Dispatcher.addEventListener(EventName.LeaveScene, this.onLeaveScene);
            Dispatcher.addEventListener(EventName.ArenaClearCd, this.onArenaClearCd);
            NetDispatcher.addCmdListener(ServerCommand.ArenaWillOpenCommand, this.onArenaWillOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaOpenCommand, this.onArenaOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaCloseCommand, this.onArenaCloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaMyArenaInfoUpdate, this.onArenaMyArenaInfoUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaMatchCommand, this.onArenaMatchHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaResultCommand, this.onArenaResultHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaStartCommand, this.onArenaStartHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaTopListUpdate, this.onTopListUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaBattlesCommand, this.onArenaBattlesHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaWaitingNumCommand, this.onArenaWaitingNumHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaFinaRingsCommand, this.onArenaFinaRingsHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaSignUpStateCommand, this.onArenaSignUpStateHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ArenaMoneyCommand, this.onArenaMoneyUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ArenaModeCommand, this.onArenaModeHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGroupCommand, this.onArenaGroupCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddArenaMark, this.onActiveAddArenaMarkHandler);
            return;
        }

        /* internal  */onEnterWaitRoom(arg1: DataEvent): void
        {
            GameProxy.arena.enterArena(true, 0);
            return;
        }

        /* internal  */onArenaJoin(arg1: DataEvent): void
        {
            var loc1=arg1.data as JoinData;
            var loc2=Boolean(arg1.data);
            if (!cache.arena.isOpen) 
            {
                MsgManager.showRollTipsMsg(Language.getString(42200));
                return;
            }
            GameProxy.arena.signUpArena(loc1.isCancelJoin, loc1.arenaMode);
            return;
        }

        /* internal  */onGetMyArenaInfo(arg1: DataEvent): void
        {
            GameProxy.arena.getMyArenaInfo();
            return;
        }

        /* internal  */onIsAccept(arg1: DataEvent): void
        {
            var loc1=Boolean(arg1.data);
            if (loc1) 
            {
                GameProxy.arena.enterArena(false, 0);
            }
            else 
            {
                GameProxy.arena.giveup();
            }
            return;
        }

        /* internal  */onShowArenaWindow(arg1: DataEvent): void
        {
            (view as IArenaModule).showArenaWindow();
            return;
        }

        /* internal  */onArenaExit(arg1: DataEvent): void
        {
            Alert.show(Language.getString(42201), null, Alert.OK | Alert.CANCEL, null, this.onExitArena);
            return;
        }

        /* internal  */onArenaExitNow(arg1: DataEvent): void
        {
            this.exitArena();
            return;
        }

        /* internal  */onExitArena(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.exitArena();
            }
            return;
        }

        /* internal  */exitArena(): void
        {
            GameProxy.arena.enterArena(true, 0);
            return;
        }

        /* internal  */disposeView(): void
        {
            (view as IArenaModule).clean();
            return;
        }

        /* internal  */onActiveAddArenaMarkHandler(arg1: SActiveToClient): void
        {
            if (_view) 
            {
                (view as IArenaModule).updateActiveAdd(arg1);
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (GameMapUtil.curMapState.isArenaWaitingRoom || GameMapUtil.isArena()) 
            {
                this._isInArenaCopy = true;
                LaterUtil.stopCallLater(LaterUtil.Arena);
            }
            else if (this._isInArenaCopy) 
            {
                LaterUtil.addCallLater(LaterUtil.Arena, this.disposeView);
                this._isInArenaCopy = false;
            }
            if (GameMapUtil.isArena()) 
            {
                this._isInAreaMap = true;
                (view as IArenaModule).enterArena();
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaClearCd));
            }
            else if (this._isInAreaMap) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
                (view as IArenaModule).leaveArena();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Role_Relive_City));
                this._isInAreaMap = false;
                cache.arena.arenaGroup = null;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaExitMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
                (view as IArenaModule).updateGroupView(null);
            }
            return;
        }

        /* internal  */clearCd(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Skill_CoolDown_Clear));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ItemMomentDrug_CoolDown_Clear));
            return;
        }

        /* internal  */onRefreshTopList(arg1: DataEvent): void
        {
            var loc1=arg1.data as PageData;
            GameProxy.arena.getArenaTopList(loc1.pageNum, loc1.pageSize, loc1.showMyPos);
            return;
        }

        /* internal  */onGetArenaBattles(arg1: DataEvent): void
        {
            GameProxy.arena.getArenaBattles();
            return;
        }

        /* internal  */onLookBattle(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            GameProxy.arena.enterArena(false, loc1);
            return;
        }

        /* internal  */onGetFinaRings(arg1: DataEvent): void
        {
            GameProxy.arena.getArenaFinaRings();
            return;
        }

        /* internal  */onLeaveScene(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onArenaClearCd(arg1: DataEvent): void
        {
            this.clearCd();
            return;
        }

        /* internal  */onArenaWillOpenHandler(arg1: Object): void
        {
            if (cache.role.entityInfo.level >= LevelLimitDefin.ArenaMinLevel) 
            {
                (view as IArenaModule).areaTips();
            }
            return;
        }

        /* internal  */onArenaOpenHandler(arg1: SArenaOpen): void
        {
            (view as IArenaModule).open(arg1);
            return;
        }

        /* internal  */opentArenaDetailPanelHander(arg1: Object /* flash.events.Event */): void
        {
            if (!this._arenaDetailControler) 
            {
                this._arenaDetailControler = new ArenaDetailControler();
            }
            this._arenaDetailControler.showView();
            return;
        }

        /* internal  */onArenaCloseHandler(arg1: Object): void
        {
            (view as IArenaModule).close();
            if (this._arenaDetailControler) 
            {
                this._arenaDetailControler.dispose();
                this._arenaDetailControler = null;
            }
            return;
        }

        /* internal  */onArenaMyArenaInfoUpdateHandler(arg1: Object): void
        {
            (view as IArenaModule).updateMyArenaInfo(cache.arena.arenaMyArenainfo);
            return;
        }

        /* internal  */onArenaMatchHandler(arg1: SArenaMatch): void
        {
            (view as IArenaModule).arenaMatch(arg1);
            return;
        }

        /* internal  */onArenaStartHandler(arg1: SArenaStart): void
        {
            (view as IArenaModule).arenaStart(arg1);
            MsgManager.showTaskTarget(Language.getString(42202));
            return;
        }

        /* internal  */onTopListUpdateHandler(arg1: Object): void
        {
            (view as IArenaModule).arenaUpdateTopList(cache.arena.arenaTopList, cache.arena.arenaPageNum);
            return;
        }

        /* internal  */onArenaBattlesHandler(arg1: SArenaBattles): void
        {
            (view as IArenaModule).arenaUpdateBattles(arg1.battles);
            return;
        }

        /* internal  */onArenaWaitingNumHandler(arg1: SArenaWaitingNum): void
        {
            (view as IArenaModule).arenaUpdateWaitingNum(arg1);
            return;
        }

        /* internal  */onArenaFinaRingsHandler(arg1: SArenaFinaRings): void
        {
            (view as IArenaModule).arenaUpdateFinaRings(arg1);
            return;
        }

        /* internal  */onArenaSignUpStateHandler(arg1: SArenaSignUpState): void
        {
            (view as IArenaModule).updateSignUpState(arg1);
            return;
        }

        /* internal  */onArenaResultHandler(arg1: SArenaResult): void
        {
            if (arg1.isBattler) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaClearCd));
            }
            (view as IArenaModule).arenaResult(arg1);
            return;
        }

        /* internal  */onMoneyUpdate(arg1: Object=null): void
        {
            if (this.isInitView()) 
            {
                (view as IArenaModule).updateMyArenaMoney(cache.role.money.arena);
            }
            return;
        }

        /* internal  */onArenaMoneyUpdate(arg1: SArenaMoney): void
        {
            (view as IArenaModule).tipsArenaMoney(arg1);
            return;
        }

        /* internal  */onArenaModeHandler(arg1: SArenaMode): void
        {
            (view as IArenaModule).arenaMode = arg1;
            return;
        }

        private /* var */_arenaModule: IArenaModule;

        private /* var */_arenaDetailControler: ArenaDetailControler;

        private /* var */_isInAreaMap: Boolean=false;

        private /* var */_isInArenaCopy: Boolean=false;
    }
